#include "StompedReality/Core/Graphics/RenderFunction.h"
#include "StompedReality/Core/Graphics/RenderGroup.h"
#include <algorithm>
namespace StompedReality
{

namespace Graphics
{

RenderGroup::RenderGroup ( )
{
  this->m_RenderKey = 0;
}

RenderGroup::~RenderGroup ( )
{
  for ( RenderFunctionList::iterator iter = this->m_RenderFunctions.begin( );
        iter != this->m_RenderFunctions.end( );
        ++iter )
  {
    delete (*iter);
  }
}

void RenderGroup::SetRenderOrder ( unsigned int renderKey )
{
  this->m_RenderKey = renderKey;
}

unsigned int RenderGroup::GetRenderOrder ( )
{
  return this->m_RenderKey;
}

void RenderGroup::Draw ( StompedReality::IGame* game )
{
  for ( RenderFunctionList::iterator iter = this->m_RenderFunctions.begin( );
        iter != this->m_RenderFunctions.end( );
        ++iter )
  {
    (*iter)->CopyRenderData( );
    (*iter)->Draw ( game );
  }
}

void RenderGroup::SortFunctions( )
{
  std::sort( this->m_RenderFunctions.begin( ), this->m_RenderFunctions.end( ),
             [&]( StompedReality::Graphics::RenderFunction* a, StompedReality::Graphics::RenderFunction* b )->int
             {
               return a->GetRenderOrder( ) < b->GetRenderOrder( );
             } );
}

void RenderGroup::AddRenderFunction ( StompedReality::Graphics::RenderFunction* function )
{
  this->m_RenderFunctions.push_back ( function );
}

}

}

